#!/usr/bin/perl

print "Gb tool analyser. \n";

open INPUT, "<", "case59.txt" or die $!;
#open $fin, "<", "log.txt" or die $!;
open $fout, ">", "output_case59.txt";

$line_number = 0;
$line = 1;
@array;

@array = <INPUT>;

close ($fin);

print "Enter the NSEI: ", "\n";
$NSEI = <>;
chomp ($NSEI);
$time_string = localtime;
print $fout "NSEI: ", $NSEI," (",hex ($NSEI),"d)", " "x8,$time_string, "\n";
print $fout "\n";


%alarm_hash;
@array_alarm;

for ($i = 0; $i < scalar(@array); $i++)	
{
	chomp (@array[$i]);
	if (@array[$i] =~ /$NSEI/i and (@array[$i-2] =~ /ALARM/ or @array[$i-2] =~ /CANCEL/) and @array[$i-1] =~ /3025/ )
	{
#		print @array[$i-3], "\n";
		@string1 = split /\s+/,@array[$i-3];
#		$alarm_hash{type} = @string1[1];
#		$alarm_hash{exchange} = @string1[2];
#		$alarm_hash{computer} = @string1[3];
#		$alarm_hash{equipment} = @string1[4];
#		$alarm_hash{date} = @string1[5];
#		$alarm_hash{hour} = @string1[6];
		
#		print @array[$i-2], "\n";
		@string2 = split /\s+/,@array[$i-2];

#		$alarm_hash{urgency} = @string2[0];
#		$alarm_hash{printout} = @string2[1];
#		print $alarm_hash{printout}, "\n";
#		$alarm_hash{object} = @string2[2];
#		$alarm_hash{position} = @string2[3];
#		$alarm_hash{issuer} = @string2[4];

#		print @array[$i-1], "\n";
#		print $fout @array[$i-1], "\n";
		@string3 = split /\s+/,@array[$i-1];
		
#		$alarm_hash{consecutive_num} = @string3[1];
#		$alarm_hash{alarm_number} = @string3[2];
#		$alarm_hash{text} = @string3[3].@string3[4].@string3[5].@string3[6].@string3[7].@string[8].@string[9];
		$text_alarm = @string3[3].@string3[4].@string3[5].@string3[6].@string3[7].@string[8].@string[9];

#		push @array_alarm, alarm_hash;		


#		print @array[$i], "\n";
#		print $fout @array[$i], "\n";
		@string4 = split /\s+/,@array[$i];

#		$alarm_hash{psei} = @string4[1];
#		$alarm_hash{nsei} = @string4[2];
#		$alarm_hash{pcu_id} = @string4[3];
#		$alarm_hash{nsvci} = @string4[4];
#		$alarm_hash{fr_bearer_ch} = @string4[5];
#		$alarm_hash{ip_adr} = hex(@string4[6]).".".hex(@string4[7]).".".hex(@string4[8]).".".hex(@string4[9]);
		$ip = hex(@string4[6]).".".hex(@string4[7]).".".hex(@string4[8]).".".hex(@string4[9]);
#		$alarm_hash{port} = hex(@string4[22]);

		push (@array_alarm, {type => @string1[1], exchange => @string1[2], computer => @string1[3], equipment => @string1[4], date => @string1[5], 
		hour => @string1[6], urgency => @string2[0], printout => @string2[1], object => @string2[2], position => @string2[3], issuer => @string2[4],
		consecutive_num => @string3[1], alarm_number => @string3[2], text => $text_alarm, psei => hex(@string4[1]), nsei => hex(@string4[2]),
		pcu_id => @string4[3], nsvci => hex(@string4[4]), fr_bearer_ch => @string4[5],ip_adr => $ip, port => hex(@string4[22]) });		
		
		
	}

	if (@array[$i] =~ /$NSEI/i and (@array[$i-2] =~ /ALARM/ or @array[$i-2] =~ /CANCEL/) and @array[$i-1] =~ /3020/ )
	{
		@string1 = split /\s+/,@array[$i-3];
		@string2 = split /\s+/,@array[$i-2];
		@string3 = split /\s+/,@array[$i-1];
		$text_alarm = @string3[3].@string3[4].@string3[5].@string3[6].@string3[7];
		@string4 = split /\s+/,@array[$i];
#		$alarm_hash{erorr_status} = @string4[6];
		$ip = hex(@string4[7]).".".hex(@string4[8]).".".hex(@string4[9]).".".hex(@string4[10]);
#		$alarm_hash{port} = hex(@string4[23]);

		push (@array_alarm, {type => @string1[1], exchange => @string1[2], computer => @string1[3], equipment => @string1[4], date => @string1[5], 
		hour => @string1[6], urgency => @string2[0], printout => @string2[1], object => @string2[2], position => @string2[3], issuer => @string2[4],
		consecutive_num => @string3[1], alarm_number => @string3[2], text => $text_alarm, psei => @string4[1], nsei => hex(@string4[2]), pcu_id => @string4[3]		     ,nsvci => hex(@string4[4]), fr_bearer_ch => @string4[5],error_status => @string4[6], ip_adr => $ip, port => hex(@string4[23]) });		
	}

	if (@array[$i] =~ /$NSEI/i and (@array[$i-2] =~ /ALARM/ or @array[$i-2] =~ /CANCEL/) and @array[$i-1] =~ /3019/ )
	{
		@string1 = split /\s+/,@array[$i-3];
		@string2 = split /\s+/,@array[$i-2];
		@string3 = split /\s+/,@array[$i-1];
		$text_alarm = @string3[3].@string3[4].@string3[5].@string3[6];
		@string4 = split /\s+/,@array[$i];

		push (@array_alarm, {type => @string1[1], exchange => @string1[2], computer => @string1[3], equipment => @string1[4], date => @string1[5], 
		hour => @string1[6], urgency => @string2[0], printout => @string2[1], object => @string2[2], position => @string2[3], issuer => @string2[4],
		consecutive_num => @string3[1], alarm_number => @string3[2], text => $text_alarm, psei => hex(@string4[1]), nsei => hex(@string4[2]), 
		pcu_id => @string4[3]});		
	}

	if (@array[$i] =~ /$NSEI/i and (@array[$i-2] =~ /ALARM/ or @array[$i-2] =~ /CANCEL/) and @array[$i-1] =~ /3030/ )
	{
		@string1 = split /\s+/,@array[$i-3];
		@string2 = split /\s+/,@array[$i-2];
		@string3 = split /\s+/,@array[$i-1];
		$text_alarm = @string3[3].@string3[4].@string3[5].@string3[6].@string3[7].@string3[8];
		@string4 = split /\s+/,@array[$i];

		push (@array_alarm, {type => @string1[1], exchange => @string1[2], computer => @string1[3], equipment => @string1[4], date => @string1[5], 
		hour => @string1[6], urgency => @string2[0], printout => @string2[1], object => @string2[2], position => @string2[3], issuer => @string2[4],
		consecutive_num => @string3[1], alarm_number => @string3[2], text => $text_alarm, psei => hex(@string4[1]), nsei => hex(@string4[2]), 
		bcf => hex(@string4[3])	, bts => hex(@string4[4]), bvci => hex(@string4[5])});		
	}

	if (@array[$i] =~ /$NSEI/i and (@array[$i-2] =~ /ALARM/ or @array[$i-2] =~ /CANCEL/) and @array[$i-1] =~ /3031/ )
	{
		@string1 = split /\s+/,@array[$i-3];
		@string2 = split /\s+/,@array[$i-2];
		@string3 = split /\s+/,@array[$i-1];
		$text_alarm = @string3[3].@string3[4].@string3[5].@string3[6].@string3[7].@string3[8];
		@string4 = split /\s+/,@array[$i];
		if(scalar(@string1)==7)
		{
		push (@array_alarm, {type => @string1[1], exchange => @string1[2], computer => @string1[3], equipment => @string1[4], date => @string1[5], 
		hour => @string1[6], urgency => @string2[0], printout => @string2[1], object => @string2[2], position => @string2[3], issuer => @string2[4],
		consecutive_num => @string3[1], alarm_number => @string3[2], text => $text_alarm, psei => hex(@string4[1]), nsei => hex(@string4[2]), 
		bcf => hex(@string4[3])	, bts => hex(@string4[4]), bvci => hex(@string4[5]), error_status => @string4[6] });		
		}
		else
		{
		push (@array_alarm, {type => @string1[0], exchange => @string1[1], computer => @string1[2], equipment => @string1[3], date => @string1[4], 
		hour => @string1[5], urgency => @string2[0], printout => @string2[1], object => @string2[2], position => @string2[3], issuer => @string2[4],
		consecutive_num => @string3[1], alarm_number => @string3[2], text => $text_alarm, psei => hex(@string4[1]), nsei => hex(@string4[2]), 
		bcf => hex(@string4[3])	, bts => hex(@string4[4]), bvci => hex(@string4[5]), error_status => @string4[6] });		
		
		}
	}

	if (@array[$i] =~ /$NSEI/i and (@array[$i-2] =~ /ALARM/ or @array[$i-2] =~ /CANCEL/) and @array[$i-1] =~ /3026/ )
	{
		@string1 = split /\s+/,@array[$i-3];
		@string2 = split /\s+/,@array[$i-2];
		@string3 = split /\s+/,@array[$i-1];
		$text_alarm = @string3[3].@string3[4].@string3[5].@string3[6].@string3[7].@string3[8];
		@string4 = split /\s+/,@array[$i];

		push (@array_alarm, {type => @string1[1], exchange => @string1[2], computer => @string1[3], equipment => @string1[4], date => @string1[5], 
		hour => @string1[6], urgency => @string2[0], printout => @string2[1], object => @string2[2], position => @string2[3], issuer => @string2[4],
		consecutive_num => @string3[1], alarm_number => @string3[2], text => $text_alarm, psei => hex(@string4[1]), nsei => hex(@string4[2]), 
		,error_status => @string4[4] });		
	}
}

print $fout "Type    Printout Alarm   PSEI  NSEI    NSVC    BCSU      PCU         Date          Time           IP              Port    CAUSE     BCF      BVCI\n";
print $fout "-----------------------------------------------------------------------------------------------------------------------------------------\n";

for ($index = 0; $index < scalar (@array_alarm); $index++)
{
	if ($array_alarm[$index]{alarm_number} == 3025)
	{ 
		print $fout $array_alarm[$index]{type}," "x4,$array_alarm[$index]{printout}, " "x4,$array_alarm[$index]{alarm_number}," "x4,
		$array_alarm[$index]{psei}," "x4,$array_alarm[$index]{nsei}," "x4, $array_alarm[$index]{nsvci}," "x4, $array_alarm[$index]{object}," "x4,
		$array_alarm[$index]{issuer}," "x4,$array_alarm[$index]{date}," "x4,$array_alarm[$index]{hour}," "x4,$array_alarm[$index]{ip_adr}," "x4,
		$array_alarm[$index]{port},"\n";
	}

	if ($array_alarm[$index]{alarm_number} == 3020)
	{ 
		print $fout $array_alarm[$index]{type}," "x4,$array_alarm[$index]{printout}," "x4,$array_alarm[$index]{alarm_number}," "x4,
		$array_alarm[$index]{psei}," "x4,$array_alarm[$index]{nsei}," "x4,$array_alarm[$index]{nsvci}," "x4,$array_alarm[$index]{object}," "x4,
		$array_alarm[$index]{issuer}," "x4,$array_alarm[$index]{date}," "x4,$array_alarm[$index]{hour}," "x4,$array_alarm[$index]{ip_adr}," "x4,
		$array_alarm[$index]{port}, " "x4,$array_alarm[$index]{error_status},"\n";
	}

	if ($array_alarm[$index]{alarm_number} == 3019)
	{ 
		print $fout $array_alarm[$index]{type}," "x4,$array_alarm[$index]{printout}," "x4,$array_alarm[$index]{alarm_number}," "x4,
		$array_alarm[$index]{psei}," "x4,$array_alarm[$index]{nsei}," "x12,$array_alarm[$index]{object}," "x4,$array_alarm[$index]{issuer}," "x4,
		$array_alarm[$index]{date}," "x4,$array_alarm[$index]{hour},"\n";
	}

	if ($array_alarm[$index]{alarm_number} == 3030)
	{ 
		print $fout $array_alarm[$index]{type}," "x4,$array_alarm[$index]{printout}," "x4,$array_alarm[$index]{alarm_number}," "x4,
		$array_alarm[$index]{psei}," "x4,$array_alarm[$index]{nsei}," "x12,$array_alarm[$index]{object}," "x4,$array_alarm[$index]{issuer}," "x4,
		$array_alarm[$index]{date}," "x4,$array_alarm[$index]{hour}," "x4,"-"x12," "x4," "x18,$array_alarm[$index]{bcf}," "x3, 
		$array_alarm[$index]{bvci},"\n";
	}

	if ($array_alarm[$index]{alarm_number} == 3031)
	{	
		if(!$array_alarm[$index]{type})
		{ 
		print $fout $array_alarm[$index]{type}," "x10,$array_alarm[$index]{printout}," "x4,$array_alarm[$index]{alarm_number}," "x4,
		$array_alarm[$index]{psei}," "x4,$array_alarm[$index]{nsei}," "x12,$array_alarm[$index]{object}," "x4,$array_alarm[$index]{issuer}," "x4,
		$array_alarm[$index]{date}," "x4,$array_alarm[$index]{hour}," "x4, "-"x12," "x4," "x10,$array_alarm[$index]{error_status}," "x7,
		$array_alarm[$index]{bcf}," "x7,$array_alarm[$index]{bvci},"\n";
		}
		else
		{
		print $fout $array_alarm[$index]{type}," "x4,$array_alarm[$index]{printout}," "x4,$array_alarm[$index]{alarm_number}," "x4,
		$array_alarm[$index]{psei}," "x4,$array_alarm[$index]{nsei}," "x12,$array_alarm[$index]{object}," "x4,$array_alarm[$index]{issuer}," "x4,
		$array_alarm[$index]{date}," "x4,$array_alarm[$index]{hour}," "x4, "-"x12," "x4," "x10,$array_alarm[$index]{error_status}," "x7,
		$array_alarm[$index]{bcf}," "x7,$array_alarm[$index]{bvci},"\n";

		}
	}

	if ($array_alarm[$index]{alarm_number} == 3026)
	{ 
		print $fout $array_alarm[$index]{type}," "x4,$array_alarm[$index]{printout}," "x4,$array_alarm[$index]{alarm_number}," "x4,
		$array_alarm[$index]{psei}," "x4,$array_alarm[$index]{nsei}," "x12,$array_alarm[$index]{object}," "x4,$array_alarm[$index]{issuer}," "x4,
		$array_alarm[$index]{date}," "x4,$array_alarm[$index]{hour}," "x4, "-"x12," "x4," "x9,$array_alarm[$index]{error_status}, "\n";
	}
}
 

close ($fout);
